﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Admin.Core.Attributes;
using Admin.Core.Common.Auth;
using Admin.Core.Common.Configs;
using Admin.Core.Common.Helpers;
using Admin.Core.Common.Input;
using Admin.Core.Common.Output;
using Admin.Core.Common.ParamInput;
using Admin.Core.Service.Quality.CheckReport;
using Admin.Core.Service.Quality.CheckReport.Input;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace Admin.Core.Controllers.Quality
{
    public class CheckResportController : AreaController
    {
        private readonly ICheckReportService _checkReportService;
        private readonly UploadConfig _uploadConfig;
        private readonly IUser _user;
        private readonly UploadHelper _uploadHelper;
        public CheckResportController(ICheckReportService checkReportService
            ,UploadHelper uploadHelper
            , IUser user
            , IOptionsMonitor<UploadConfig> uploadConfig)
        {
            _uploadConfig = uploadConfig.CurrentValue;
            _uploadHelper = uploadHelper;
            _user = user;
            _checkReportService = checkReportService;
        }

        /// <summary>
        /// 查询单条校验信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IResponseOutput> Get(long id)
        {
            return await _checkReportService.GetAsync(id);
        }


        /// <summary>
        /// 查询分页校验信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> GetPage(PageInput<CheckReportFilterInput> model)
        {
            return await _checkReportService.PageAsync(model);
        }

        /// <summary>
        /// 新增校验数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> Add(CheckReportAddInput input)
        {
            return await _checkReportService.AddAsync(input);
        }

        /// <summary>
        /// 修改校验数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IResponseOutput> Update(CheckReportUpdateInput input)
        {
            return await _checkReportService.UpdateAsync(input);
        }

        /// <summary>
        /// 删除校验数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IResponseOutput> SoftDelete(long id)
        {
            return await _checkReportService.SoftDeleteAsync(id);
        }

        /// <summary>
        /// 上传头像
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Login]
        public async Task<IResponseOutput> FileUpload([FromForm] IFormFile file)
        {
            var config = _uploadConfig.Calfile;
            var res = await _uploadHelper.UploadAsync(file, config, new { _user.Id });
            if (res.Success)
            {
                return ResponseOutput.Ok(res.Data.FileRelativePath);
            }

            return ResponseOutput.NotOk("上传失败！");
        }
    }
}
